package protocol

import (
	"JoRedis/constant"
	"bytes"
	"strconv"
)

type MultiBulkReply struct {
	ArgBytesSlc [][]byte
}

func MakeMultiBulkReply(argBytesSlc [][]byte) *MultiBulkReply {
	return &MultiBulkReply{ArgBytesSlc: argBytesSlc}
}

func (this *MultiBulkReply) ToBytes() []byte {
	argLen := len(this.ArgBytesSlc)
	var buf bytes.Buffer
	buf.WriteString("*" + strconv.Itoa(argLen) + constant.CRLF)
	for _, argBytes := range this.ArgBytesSlc {
		if argBytes == nil {
			buf.WriteString("$-1" + constant.CRLF)
		} else {
			buf.WriteString("$" + strconv.Itoa(len(argBytes)) + constant.CRLF)
			buf.WriteString(string(argBytes) + constant.CRLF)
		}
	}
	return buf.Bytes()
}
